package com.topisv.tms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.PipeStock;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.Storage;
import com.topisv.tms.repository.AreaGroupDao;
import com.topisv.tms.repository.PipeStockDao;
import com.topisv.tms.repository.StorageDao;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
//默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class StorageService extends BaseService{
	private StorageDao storageDao;
	
	private AreaGroupDao areaGroupDao;
	
	@Autowired
	private PipeStockDao pipeStockDao;
	
	public List<Storage> findByAreaGroup(Long areaGroupId,String type){
		return storageDao.findByAreaGroup(areaGroupId, type);
	}
	
	public Storage findByCode(String code){
		return storageDao.findByCode(code);
	}
	
	@Autowired
	public void setStorageDao(StorageDao storageDao) {
		this.storageDao = storageDao;
	}

	@Autowired
	public void setAreaGroupDao(AreaGroupDao areaGroupDao) {
		this.areaGroupDao = areaGroupDao;
	}

	public Storage getStorage(Long id) {
		return storageDao.findOne(id);
	}
	
	@Transactional(readOnly = false)
	public void saveStorage(Storage entity) {
		int num = entity.getNum();
		for(int i=0; i<num; i++)
		{
			int _num = i+1;
			String shelf_code = "";
			if( _num < 10)
			{
				shelf_code = "00" + _num;
			}
			else if( _num >= 10 && _num < 100)
			{
				shelf_code = "0" + _num;
			}
			else
			{
				shelf_code = "" + _num;
			}
			PipeStock pipeStock = new PipeStock();
			pipeStock.setPartition_code("");
			pipeStock.setAreagroup_code(entity.getAreaGroup().getGroupCode());
			pipeStock.setCustomer_code("");
			pipeStock.setStock_name(shelf_code);
			pipeStock.setStock_code(entity.getCode() + shelf_code);
			pipeStock.setSku_id("");
			pipeStock.setPipe_code("");
			pipeStock.setStock_status("0");
			pipeStock.setStock_type("0");
			pipeStock.setStorage_code(entity.getCode());
			pipeStock.setShelf_code("");
			pipeStockDao.save(pipeStock);
		}
		storageDao.save(entity);
	}
	
	@Transactional(readOnly = false)
	public void deleteStorage(Long id) {
		Storage storage = storageDao.findOne(id);
		if(null != storage)
		{
			pipeStockDao.deleteByStorageCode(storage.getCode());
		}
		storageDao.delete(id);
	}
	
	public List<Storage> getAllStorage() {
		return (List<Storage>) storageDao.findAll();
	}
	
	public List<Storage> getStorageByAreaGroupId(Long id) {
		return storageDao.getByAreaGroupId(id);
	}
	
	public List<PipeStock> getPipeStock(String storage_code) {
		return pipeStockDao.findByStorageCode(storage_code);
	}
	
	/**
	 * 分页显示表内所有行.
	 */
	public Page<Storage> getStorages(QueryParamHolder paramHolder) {
		return findByJson(paramHolder, storageDao, Storage.class);
	}
	
	public List<AreaGroup> getAllAreaGroups() {
		return (List<AreaGroup>) areaGroupDao.findAll();
	}
}
